﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SocialLearning
{
    public partial class UserUpdateForm : Form
    {
        #region  Fields
        UAC_Account UpdateUser = new UAC_Account();
        #endregion

        #region Constructors
        public UserUpdateForm()
        {
            InitializeComponent();
            SetComboBox();
        }
        internal UserUpdateForm(UAC_Account user)
        {
            InitializeComponent();
            SetComboBox();
            SetupForm(user);
            if (user.EducationBackground != null)
            {
                populateInstatutionList(user.EducationBackground.Institution);
            }

            UpdateUser = user;
        }

        #endregion

        #region Setup Window Methods
        private void SetComboBox()
        {
            cbxGender.DataSource = Enum.GetValues(typeof(UAC_Gender));
            cbxGender.SelectedIndex = 0;
            cbxEthnic.DataSource = Enum.GetValues(typeof(UAC_EthnicGroup));
            clbxDisabilities.DataSource = Enum.GetValues(typeof(UAC_Disabilities));
        }
        private void SetupForm(UAC_Account user)
        {
            // Basic
            txtFName.Text = user.BasicAccountDetails.Firstname;
            txtSName.Text = user.BasicAccountDetails.Lastname;
            txtID.Text = user.BasicAccountDetails.ID;
            cbxGender.Text = user.BasicAccountDetails.Gender.ToString();
            txtEmail.Text = user.BasicAccountDetails.Email;
            //Advanced
            dtpDOB.Value = user.AdvancedAccountDetails.DateOfBirth;
            txtHomeLang.Text = user.AdvancedAccountDetails.HomeLanguage;
            txtNationality.Text = user.AdvancedAccountDetails.Nationality;
            txtHomeAdd.Text = user.AdvancedAccountDetails.HomeAddress;
            txtCity.Text = user.AdvancedAccountDetails.City;
            txtCountry.Text = user.AdvancedAccountDetails.Country;
            txtPostalCode.Text = user.AdvancedAccountDetails.Code;
            txtMedAid.Text = user.AdvancedAccountDetails.MedicalAid;
            txtMedAidNum.Text = user.AdvancedAccountDetails.MedicalAidNumber;
            cbxEthnic.Text = user.AdvancedAccountDetails.EthnicGroup.ToString();
            //Market
            cbxMedium.Text = user.MarketingDetails.MarketingMedium;
            txtComment.Text = user.MarketingDetails.MarketingComment;
            dtpsubmission.Value = user.MarketingDetails.MarketingDateOfSubmission;
            cbxOccupation.Text = user.MarketingDetails.CurrentStatus.ToString();
            txtHishSchool.Text = user.MarketingDetails.HighschoolDetails;
            //educational
        }

        #endregion

        #region Create User Methods
        private void SetBasic()
        {
            UAC_Gender gender = (UAC_Gender)cbxGender.SelectedItem;
            UpdateUser.BasicAccountDetails = new UAC_BasicDetails(txtFName.Text, txtSName.Text, gender, txtID.Text, txtEmail.Text, txtcontact.Text, cbxUsertype.SelectedText, txtfamID.Text);
        }
        private void SetAdvanced()
        {
            UAC_EthnicGroup ethnic;
            UAC_Disabilities dissability;

            if (cbxGender.SelectedIndex != -1)
            {
                ethnic = (UAC_EthnicGroup)cbxGender.SelectedItem;
            }
            else
            {
                ethnic = UAC_EthnicGroup.Other;
            }
            if (clbxDisabilities.SelectedIndex != -1)
            {
                dissability = (UAC_Disabilities)clbxDisabilities.SelectedItem;
            }
            else
            {
                dissability = UAC_Disabilities.Other;
            }
            UpdateUser.AdvancedAccountDetails = new UAC_AdvancedDetails(dtpDOB.Value, txtHomeLang.Text, txtNationality.Text, txtHomeAdd.Text, txtPostalAdd.Text, txtCity.Text, txtCountry.Text, txtPostalCode.Text, txtMedAid.Text, txtMedAidNum.Text, ethnic, dissability);
        }
        private void SetMarket()
        {
            UpdateUser.MarketingDetails = new UAC_MarketingDetails(cbxMedium.SelectedText, txtComment.Text, dtpsubmission.Value, (UAC_OccupationStatus)cbxOccupation.SelectedValue, txtHishSchool.Text);
        }
        private void populateInstatutionList(List<I_UAC_InstitutionBackground> edulist)
        {
            lbxInsList.Items.Clear();
            foreach (I_UAC_InstitutionBackground edu in edulist)
            {
                lbxInsList.Items.Add(edu.InstitutionName);
            }
        }
        #endregion

        #region Password Validation
        private bool ValidatePassword()
        {
            if (!string.IsNullOrEmpty(txtOldPass.Text) && !string.IsNullOrEmpty(txtNewPass.Text))
            {
                if (UpdateUser.Password == txtOldPass.Text)
                {
                    if (txtNewPass.Text == txtRePass.Text)
                    {
                        UpdateUser.Password = txtNewPass.Text;
                        return true;
                    }
                    else
                    {
                        MessageBox.Show(this, "New Passwords do not match");
                        return false;
                    }
                }
                else
                {
                    MessageBox.Show(this, "Old Password incorrect");
                    return false;
                }
            }
            if (string.IsNullOrEmpty(txtOldPass.Text))
            {
                return true;
            }
            // else
            // {
            //      MessageBox.Show(this, "Please Enter old and new passwords");
            //      return false;
            //  }
            return false; // Double check
        }
        #endregion

        #region Buttons
        private void btnInstatutionAdd_Click(object sender, EventArgs e)
        {

            I_UAC_EducationalBackground educationalBackground;
            if (UpdateUser.EducationBackground == null)
            {
                educationalBackground = new UAC_EducationalBackground();
            }
            else educationalBackground = UpdateUser.EducationBackground;
            if (!string.IsNullOrEmpty(txtInstatution.Text))
            {
                UAC_InstitutionBackground institution = new UAC_InstitutionBackground(txtInstatution.Text, dtpStart.Value, dtpEnd.Value);
                List<I_UAC_InstitutionSubjectResult> subs = EducationDetailsForm.Show(institution);
                if (subs != null)
                {
                    institution.InstitutionSubjects = subs;
                    educationalBackground.Institution.Add(institution);
                    UpdateUser.EducationBackground = educationalBackground;
                    lbxInsList.Items.Add(institution.InstitutionName);
                }
                else
                {
                    MessageBox.Show(this, "Instatution Not Added");
                }
            }
            else
            {
                MessageBox.Show(this, "Please Enter A Instatution Name");
            }
        }
        private void btnEditEdu_Click(object sender, EventArgs e)
        {
            EducationUpdateForm.Show(UpdateUser.EducationBackground.Institution);
            populateInstatutionList(UpdateUser.EducationBackground.Institution);

        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            SetBasic();
            SetAdvanced();
            SetMarket();
            if (ValidatePassword())
            {
                UAC_UserManagement um = new UAC_UserManagement();
                um.Update(UpdateUser);
                MessageBox.Show(this, "User Updated");
            }
        }
        #endregion
    }
}
